package top.deepdesigner.controller.promotion;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.deepdesigner.annotation.RequiresPermissionsDesc;
import top.deepdesigner.pojo.DtsGrouponRules;
import top.deepdesigner.service.DtsGrouponRulesService;
import top.deepdesigner.service.DtsGrouponService;
import top.deepdesigner.util.ResponseUtil;
import top.deepdesigner.vo.CommonListVO;
import top.deepdesigner.vo.GroupOnRecordVO;

import java.util.HashMap;
import java.util.List;

/**
 * Start up <br/>
 * Created with IntelliJ IDEA. <br/>
 * Project Name: shop_manage <br/>
 * Author: duanruiqing <br/>
 * DateTime: 2022/9/23 星期五 20:34:43 <br/>
 */

@RestController
@CrossOrigin
@RequestMapping("/admin/groupon")
public class GrouponController {

  @Autowired
  private DtsGrouponRulesService grouponRulesService;

  @Autowired
  private DtsGrouponService grouponService;

  /**
   * 团购规则列表
   *
   * @param params 参数个数
   * @return {@link Object}
   */
  @RequiresPermissionsDesc(menu = {"推广管理", "团购规则"}, button = "获取列表")
  @RequiresPermissions(value = {"admin:groupon:list"})
  @GetMapping("/list")
  public Object list(@RequestParam HashMap<String, Object> params,
                     @RequestParam(defaultValue = "") Integer goodsId) {
    Integer page = Integer.parseInt((String) params.get("page"));
    Integer limit = Integer.parseInt((String) params.get("limit"));
    String sort = (String) params.get("sort");
    String order = (String) params.get("order");

    CommonListVO<DtsGrouponRules> grouponListVO = new CommonListVO<>();
    //获取分页数量
    Long count = grouponRulesService.getCouponCount();
    List<DtsGrouponRules> grouponList = grouponRulesService.getCouponList(page, limit, sort, order, goodsId);
    grouponListVO.setTotal(count);
    grouponListVO.setItems(grouponList);
    return ResponseUtil.ok(grouponListVO);
  }

  /**
   * 创建
   * 创建团购
   *
   * @param grouponRules groupon规则
   * @return {@link Object}
   */
  @RequiresPermissionsDesc(menu = {"推广管理", "团购规则"}, button = "添加")
  @RequiresPermissions(value = {"admin:groupon:create"})
  @PostMapping("/create")
  public Object create(@RequestBody DtsGrouponRules grouponRules) {
    DtsGrouponRules grouponRes = grouponRulesService.createCoupon(grouponRules);
    if (grouponRes != null) return ResponseUtil.ok(grouponRes);
    return ResponseUtil.fail(700, "商品不存在");
  }

  /**
   * 更新
   * 更新团购
   *
   * @param grouponRules groupon规则
   * @return {@link Object}
   */
  @RequiresPermissionsDesc(menu = {"推广管理", "团购规则"}, button = "修改")
  @RequiresPermissions(value = {"admin:groupon:update"})
  @PostMapping("/update")
  public Object update(@RequestBody DtsGrouponRules grouponRules) {
    DtsGrouponRules grouponRes = grouponRulesService.updateCoupon(grouponRules);
    if (grouponRes != null) return ResponseUtil.ok(grouponRes);
    return ResponseUtil.fail();
  }

  /**
   * 删除
   * 删除团购
   *
   * @param grouponRules groupon规则
   * @return {@link Object}
   */
  @RequiresPermissionsDesc(menu = {"推广管理", "团购规则"}, button = "删除")
  @RequiresPermissions(value = {"admin:groupon:delete"})
  @PostMapping("/delete")
  public Object delete(@RequestBody DtsGrouponRules grouponRules) {
    grouponRulesService.deleteCoupon(grouponRules);
    return ResponseUtil.ok();
  }

  @RequiresPermissionsDesc(menu = {"推广管理", "团购活动"}, button = "获取列表")
  @RequiresPermissions(value = {"admin:groupon:listRecord"})
  @GetMapping("/listRecord")
  public Object listRecord(@RequestParam HashMap<String, Object> params,
                           @RequestParam(defaultValue = "") Integer rulesId) {
    Integer page = Integer.parseInt((String) params.get("page"));
    Integer limit = Integer.parseInt((String) params.get("limit"));
    String sort = (String) params.get("sort");
    String order = (String) params.get("order");

    CommonListVO<GroupOnRecordVO> listCommonListVO = new CommonListVO<>();
    Long couponRecordCount = grouponService.getCouponRecordCount();
    List<GroupOnRecordVO> groupOnRecordVOS = grouponService.getCouponRecordList(page, limit, sort, order, rulesId);
    listCommonListVO.setTotal(couponRecordCount);
    listCommonListVO.setItems(groupOnRecordVOS);
    return ResponseUtil.ok(listCommonListVO);
  }

}
